﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BLL;
using CommonLib;

namespace ProjectStudent
{
    public partial class StudentJoinClass : Form
    {
       
        public StudentJoinClass()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            //GetAllData();
            GetPageData();
        }
        SJoinCBLL SJoinCBLL = new SJoinCBLL();
        public void GetAllData()
        {
     
            DataTable dt = SJoinCBLL.GetData();
            dataGridView1.DataSource = dt;
        }
        public void GetPageData()
        {
            int total = SJoinCBLL.GetDataCount("");
            lb_total.Text = total.ToString();
            int pageSize = Convert.ToInt32(comboBox1.Text);
            int totalPage = (int)Math.Ceiling((double)total / pageSize);
            lb_pageCount.Text = totalPage.ToString();
            int pageNumber = Convert.ToInt32(txtCurrentPage.Text);
            DataTable dt = SJoinCBLL.GetPageData(pageNumber, pageSize, "");
            dataGridView1.DataSource = dt;
        }

        private void btnHome_Click(object sender, EventArgs e)
        {
            txtCurrentPage.Text = "1";
            GetPageData();
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            int pageNumber = Convert.ToInt32(txtCurrentPage.Text);
            if (pageNumber > 1)
            {
                pageNumber--;
                txtCurrentPage.Text = pageNumber.ToString();
            }
            else
            {
                MessageBox.Show("已经是第一页了！");
            }
            GetPageData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            int totalPage = Convert.ToInt32(lb_pageCount.Text);
            int pageNumber = Convert.ToInt32(txtCurrentPage.Text);
            if (pageNumber < totalPage)
            {
                pageNumber++;
                txtCurrentPage.Text = pageNumber.ToString();
            }
            else
            {
                MessageBox.Show("已经是最后一页了！");
            }
            GetPageData();
        }

        private void btnEnd_Click(object sender, EventArgs e)
        {
            txtCurrentPage.Text = lb_pageCount.Text;
            GetPageData();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtCurrentPage.Text = "1";
            GetPageData();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string keyword = txtSearch.Text;
            if(keyword == null)
            {
                GetPageData();
                return;
            }
            DataTable dt = SJoinCBLL.SearchKey(keyword);
            if(dt.Rows.Count>0)
            {
                dataGridView1.DataSource = dt;
            }
            else
            {
                dataGridView1.DataSource = null;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            //GetPageData();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            GetPageData();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "CSV 文件 (*.csv)|*.csv";
            saveFileDialog.Title = "保存为 CSV 文件";
            if(saveFileDialog.ShowDialog()== DialogResult.OK)
            {
                try
                {
                    string filePath = saveFileDialog.FileName;
                    if (SJoinCBLL.ExportStudentToCsv(filePath))
                    {
                        MessageBox.Show("导出成功！");
                    }
                    else
                    {
                        MessageBox.Show("导出失败！");
                    }

                }
                catch (Exception ex)
                {
                    
                    ////throw;
                }
            }
            //SaveFileDialog saveFileDialog = new SaveFileDialog();
            //saveFileDialog.Filter = "CSV文件 (*.csv)|*.csv";
            //saveFileDialog.Title = "请选择导出CSV文件保存位置";
            //if (saveFileDialog.ShowDialog() == DialogResult.OK)
            //{
            //    string filePath = saveFileDialog.FileName;
            //    if (SJoinCBLL.ExportStudentToCsv(filePath))
            //    {
            //        MessageBox.Show("导出成功！");
            //    }
            //    else
            //    {
            //        MessageBox.Show("导出失败！");
            //    }
            //}

        }
    }
}
